package com.xhui.vimarket.zkplus.zul.attr.mod.impl;t java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.directwebremoting.util.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Div;
import org.zkoss.zul.Label;

import com.xhui.crm.common.exception.RtManagerException;
import com.xhui.crm.common.vo.RetVO;com.xhui.vimarket.zkplusui.vimarket.zkplus.zul.attrcom.xhui.vimarket.zkplusrt com.xhui.vimarket.zkplus.zul.attr.adacom.xhui.vimarket.zkpluser;
import com.xhui.vimarket.zkpcom.xhui.vimarket.zkplusan.AttrInst;
import com.xhui.vimcom.xhui.vimarket.zkplusl.attr.bean.AttrTmpl;
import ccom.xhui.vimarket.zkplust.zkplus.zul.attr.bean.ValiVo;
import com.xhui.com.xhui.vimarket.d.AttrCompFactory;
import com.xhui.vimarketcom.xhui.vimarket.zkplusel.GroupPanel;

/**
 * @版权：福富软件 版权所有 (c) 2010
 * @文件：com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrTreeTabComp.java
 * @所含类：
 * @author: wuyx
 * @version: V1.0
 * @see:
 * @创建日期：2010-12-20
 * @功能说明：group控件
 * @修改记录： =============================================================<br>
 *        日期:2010-12-20 创建
 *        =============================================================<br>
 */
public final class AttrGroupboxComp extends AttrBaseComp {
    
    /**
     * 实例.
     */
    private static AttrGroupboxComp inst   = new AttrGroupboxComp();
    
    /**
     * 日志.
     */
    private static Logger           logger = Logger.getLogger(AttrGroupboxComp.class);
    
    /**
     * 构造函数. 
     */
    private AttrGroupboxComp() {
    }
    
    /**
     * 获取实例.
     * 
     * @return AttrBaseComp
     */
    public static AttrGroupboxComp instance() {
        return AttrGroupboxComp.inst;
    }
    
    /**
     * 生成组件.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件模板
     * @param comp 显示容器组件
     */
    @Override
    public void create(final AttrExt attrExt, final AttrTmpl attrTmpl, final Component comp) {
        super.create(attrExt, attrTmpl, comp);
        // groupbox
        //        final String sGroupboxWithComposer = attrExt.getAttrAda().getAttrTmplCompStr(
        //            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null);
        //        final GroupPanel groupbox = (GroupPanel) Executions.createComponentsDirectly(
        //            sGroupboxWithComposer, null, comp, null);
        final GroupPanel groupbox = (GroupPanel) attrExt.getAttrAda().getAttrTmplCompStr(
            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null, comp);
        groupbox.setAttribute(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
        groupbox.setAttribute(AttrBaseComp.COMP_ATTRID, attrTmpl.getAttrId());
        //groupbox.setMold("3d");
        groupbox.setAttribute(AttrBaseComp.COMP_ATTRTMPL, attrTmpl); // tmp
        groupbox.setWidth("100%");
        // caption
        // final Caption caption = new Caption();
        final Label caption = (Label) groupbox.getFirstChild().getFirstChild().getFirstChild();
        caption.setValue(attrTmpl.getName());
        // div
        final Div div = new Div();
        
        // add to zul
        // groupbox.setParent(comp);
        // caption.setParent(groupbox);
        div.setParent(groupbox);
        
        // comp log
        if (AttrGroupboxComp.logger.isDebugEnabled()) {
            attrExt.appendInnerComp("<groupbox>");
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_ATTRID, "" + attrTmpl.getAttrId());
        }
        
        // sub attr
        if (attrTmpl.getAttrTmplList() != null) {
            for (final AttrTmpl subAttrTmpl : attrTmpl.getAttrTmplList()) {
                AttrCompFactory.getInst(subAttrTmpl.getDisplayMode()).create(attrExt, subAttrTmpl,
                    div);
            }
        }
        
        // log
        if (AttrGroupboxComp.logger.isDebugEnabled()) {
            attrExt.appendInnerComp("</groupbox>");
        }
        
        super.afterCreate(attrExt, attrTmpl, comp);
    }
    
    /**
     * 设置值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param attrInstList 属性实例
     */
    @SuppressWarnings("unchecked")
    @Override
    public void setValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList, Map<String, Integer> attrViewMap) {
        super.setValue(attrExt, comp, attrInstList, attrViewMap);
        // 自身不实例化，设置下级数据
        // groupbox //div
        final Component compGroupboxDivchild = comp.getLastChild();
        final List<Component> subCompRootList = compGroupboxDivchild.getChildren();
        if (subCompRootList != null) {
            for (final Component subRootComp : subCompRootList) {
                AttrCompFactory.getInstByComponent(subRootComp).setValue(attrExt, subRootComp,
                    attrInstList, attrViewMap);
 com.xhui.vimarket.zkplus       }
    }
    
    /**
     * {@inheritcom.xhui.vimarket.zkplusee com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #setRemovedValue(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component, java.util.List)
     * @author wuyx
     * 2011-4-8 wuyx
     */
    public void setRemovedValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList) {
        super.setRemovedValue(attrExt, comp, attrInstList);
        // 自身不实例化，设置下级数据
        // groupbox //div
        final Component compGroupboxDivchild = comp.getLastChild();
        final List<Component> subCompRootList = compGroupboxDivchild.getChildren();
        if (subCompRootList != null) {
            for (final Component subRootComp : subCompRootList) {
                AttrCompFactory.getInstByComponent(subRootComp).setRemovedValue(attrExt,
                    subRootComp,com.xhui.vimarket.zkplus
            }
        }
    }
    
    /**com.xhui.vimarket.zkplusritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #setRemovedValue(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component, java.util.List)
     * @author wuyx
     * 2011-4-8 wuyx
     */
    public void setDisabledValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList) {
        super.setRemovedValue(attrExt, comp, attrInstList);
        // 自身不实例化，设置下级数据
        // groupbox //div
        final Component compGroupboxDivchild = comp.getLastChild();
        final List<Component> subCompRootList = compGroupboxDivchild.getChildren();
        if (subCompRootList != null) {
            for (final Component subRootComp : subCompRootList) {
                AttrCompFactory.getInstByComponent(subRootComp).setDisabledValue(attrExt,
                    subRootComp, attrInstList);
            }
        }
    }
    
    /**
     * 获取值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @return ArrayList<AttrInst>
     */
    @SuppressWarnings("unchecked")
    @Override
    public ArrayList<AttrInst> getValue(final AttrExt attrExt, final Component comp) {
        super.getValue(attrExt, comp);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        // 自身不实例化，取下级数据
        // groupbox //div
        final Component compGroupboxDivchild = comp.getLastChild();
        final List<Component> subCompRootList = compGroupboxDivchild.getChildren();
        if (subCompRootList != null) {
            for (final Component subRootComp : subCompRootList) {
                final ArrayList<AttrInst> subRetList = AttrCompFactory.getInstByComponent(
                    subRootComp).getValue(attrExt, subRootComp);
                if (subRetList != null) {
                    retList.addAll(subRetList);
                }
            }
        }
        return retList;
    }
    
    /**
     * 获取值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @return ArrayList<AttrInst>
     */
    @SuppressWarnings("unchecked")
    @Override
    public ArrayList<AttrInst> getNullValue(final AttrExt attrExt, final Component comp) {
        super.getValue(attrExt, comp);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        // 自身不实例化，取下级数据
        // groupbox //div
        final Component compGroupboxDivchild = comp.getLastChild();
        final List<Component> subCompRootList = compGroupboxDivchild.getChildren();
        if (subCompRootList != null) {
            for (final Component subRootComp : subCompRootList) {
                final ArrayList<AttrInst> subRetList = AttrCompFactory.getInstByComponent(
                    subRootComp).getNullValue(attrExt, subRootComp);
                if (subRetList != null) {
                    retList.addAll(subRetList);
                }
            }
        }
        return retList;
    }
    
    /**
     * 取空值属性实例.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 模板
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getNullValue(final AttrExt attrExt, final AttrTmpl attrTmpl) {
        super.getNullValue(attrExt, attrTmpl);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        
        final int sum = attrTmpl.getAttrTmplListCount();
        AttrGroupboxComp.logger.debug("subRowList size:" + sum);
        for (int i = 0; i < sum; i++) {
            final AttrTmpl subAttrTmpl = attrTmpl.getAttrTmplList(i);
            final AttrInst attrInst = attrExt.newAttrInst();
            attrInst.setAttrId(subAttrTmpl.getAttrId());
            attrInst.setJavaCode(subAttrTmpl.getJavaCode());
       com.xhui.vimarket.zkplusd(attrInst);
        }
        return retLicom.xhui.vimarket.zkplus   /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp#signRuleHint(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2012-2-16 wuyx
     */
    @Override
    public ArrayList<ValiVo> signRuleHint(AttrExt attrExt, Component comp) {
        super.signRuleHint(attrExt, comp);
        final ArrayList<ValiVo> retList = new ArrayList<ValiVo>();
        // 自身不实例化，取下级数据
        // groupbox //div
        final Component compGroupboxDivchild = comp.getLastChild();
        final List<Component> subCompRootList = compGroupboxDivchild.getChildren();
        if (subCompRootList != null) {
            for (final Component subRootComp : subCompRootList) {
                final ArrayList<ValiVo> subRetList = AttrCompFactory.getInstByComponent(
                    subRootComp).signRuleHint(attrExt, subRootComp);
                if (subRetList != null) {
                    retList.addAll(subRetList);
                }
            }
        }
        return retList;
    }
    
    /**
     * 清空值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     */
    @SuppressWarnings("unchecked")
    @Override
    public void clean(final AttrExt attrExt, final Component comp) {
        // 自身不实例化，取下级数据
        // groupbox //div
        final Component compGroupboxDivchild = comp.getLastChild();
        final List<Component> subCompRootList = compGroupboxDivchild.getChildren();
        if (subCompRootList != null) {
            for (final Component subCompRootBox : subCompRootList) {
                if (subCompRootBox != null) {
                    AttrCompFactory.getInstByComponent(subCompRootBox).clean(attrExt,
                        subCompRootBox);
                }
            }
        }
    }
    
    /**
     * 重新生成下级组件.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件根
     * @param newSubAttrTmplList 组件模板
     * @param comp 本 控件容器组件
     * @author wuyx
     * 传入本级控件根目录，替换新的模板定义，并刷新界面显示
     */
    @Override
    @SuppressWarnings("unchecked")
    public void reCreateSub(final AttrExt attrExt, AttrTmpl attrTmpl,
        final List<AttrTmpl> newSubAttrTmplList, final Component comp) {
        super.reCreateSub(attrExt, attrTmpl, newSubAttrTmplList, comp);
        // 消除下级控件
        // groupbox //div
        final Component compGroupboxDivchild = comp.getLastChild();
        final List<Component> subCompRootList = compGroupboxDivchild.getChildren();
        // String t1 = ZkUiUtil.getCompStr(compGroupboxDivchild);
        final int sum = subCompRootList == null ? 0
            : subCompRootList.size();
        if (sum > 0) {
            compGroupboxDivchild.getChildren().clear();
        }
        // 获取本级attrTmpl，并更新
        // AttrTmpl rootAttrTmpl = attrExt.getAttrSpec().getAttrTmpl();
        // AttrTmpl theThisTmpl = AttrCompUtil.getEqualAttrTmpl(rootAttrTmpl, comp);
        final AttrTmpl theThisTmpl = (AttrTmpl) comp.getAttribute(AttrBaseComp.COMP_ATTRTMPL);
        if (theThisTmpl == null) {
            throw new RtManagerException("无法查找到该控件对于模板根节点定义", this.getClass(), "reCreateSub",
                "getEqualTmplNull");
        }
        
        // 更新界面显示
        ((Div) compGroupboxDivchild).setWidth("100%");
        if (newSubAttrTmplList != null) {
            for (final AttrTmpl subAttrTmpl : newSubAttrTmplList) {
                AttrCompFactory.getInst(subAttrTmpl.getDisplayMode()).create(attrExt, subAttrTmpl,
                    compGroupboxDivchild);
            }
        }
        //String t2 = ZkUiUtil.getCompStr(compGroupboxDivchild);
    }
    
    /**
     * 设置为不允许修改.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param tflag 设置值，true/false
     */
    @Override
    public void setDisabled(AttrExt attrExt, Component comp, boolean tflag) {
        super.setDisabled(attrExt, comp, tflag);
        // 自身不实例化，取下级数据
        // groupbox //div
        final Component compGroupboxDivchild = comp.getLastChild();
        final List<Component> subCompRootList = compGroupboxDivchild.getChildren();
        if (subCompRootList != null) {
            for (final Component subCompRootBox : subCompRootList) {
                if (subCompRootBox != null) {
                    AttrCompFactory.getInstByComponent(subCompRootBox).setDisabled(attrExt,
               com.xhui.vimarket.zkpluspRootBox, tflag);
                }com.xhui.vimarket.zkplus
        }
    }
    
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp#
     * exec(com.xhui.vimarket.zkplus.zul.attr.ada.AttrExecListener)
     * @author wuyx
     * 2011-3-16 wuyx
     */
    @Override
    public void exec(final AttrExt attrExt, final Component comp, AttrExecListener attrExecListener) {
        super.exec(attrExt, comp, attrExecListener);
        if (attrExecListener == null) {
            return;
        }
        // 自身不实例化，取下级数据
        // groupbox //div
        final Component compGroupboxDivchild = comp.getLastChild();
        final List<Component> subCompRootList = compGroupboxDivchild.getChildren();
        if (subCompRootList != null) {
            for (final Component subRootComp : subCompRootList) {
                // 下级
                AttrCompFactory.getInstByComponent(subRootComp).exec(attrExt, subRootComp,
                    attrExecListener);
                /com.xhui.vimarket.zkplus       attrExecListener.onEvent(subRootComp, attrEcom.xhui.vimarket.zkplus   }
        }
    }
    
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp
     * #validate(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2011-3-22 wuyx
     */
    @Override
    public List<RetVO> validate(AttrExt attrExt, Component comp) {
        final List<RetVO> retList = new ArrayList<RetVO>();
        // 自身不实例化，取下级数据
        // groupbox //div
        final Component compGroupboxDivchild = comp.getLastChild();
        final List<Component> subCompRootList = compGroupboxDivchild.getChildren();
        if (subCompRootList != null) {
            for (final Component subRootComp : subCompRootList) {
                final List<RetVO> subRetList = AttrCompFactory.getInstByComponent(subRootComp)
                    .validate(attrExt, subRootComp);
                if (subRetList != null) {
    com.xhui.vimarket.zkplus retList.addAll(subRetList);
                }
          com.xhui.vimarket.zkplus
        return retList;
    }
    
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp
     * #refreshButtonText(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2011-3-24 wuyx
     */
    @Override
    public void refreshButtonText(AttrExt attrExt, Component comp, final List<AttrInst> attrInstList) {
        super.getValue(attrExt, comp);
        final List<RetVO> retList = new ArrayList<RetVO>();
        // 自身不实例化，取下级数据
        // groupbox //div
        final Component compGroupboxDivchild = comp.getLastChild();
        final List<Component> subCompRootList = compGroupboxDivchild.getChildren();
        if (subCompRootList != null) {
            for (final Component subRootComp : subCompRootList) {
                AttrCompFactory.getInstByComponent(subRootComp).refreshButtonText(attrExt,
                    subRootComp, attrInstList);
            }
        }
    }
    
}
